# https://www.baidu.com/s?&wd=
import requests
from bs4 import BeautifulSoup

from com.py.test.emotion.Emotion import Emotion


class ReptleBaiduComment:
    # def __init__(self):
    def start(self,word,num):
        result = []
        baidu_url = "https://www.baidu.com/s?&wd="+word+'&pn='+num+'0'
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36",
            # "Host":"www.baidu.com",
            # "Cookie":"BIDUPSID=2A48399FEC57D4C8C9699B8019B9A739; PSTM=1528371586; BAIDUID=469B57C4817EC032BBD2845CED6578DD:FG=1; H_PS_PSSID=1424_21098_29522_29521_29720_29568_29220_26350_22159; BD_UPN=16314753; H_PS_645EC=5b29o2%2Fn3cE2ewd8t%2B%2Bx1yWNnapq3S6nbSAksVw5o642Q7jvTF9YWZ0%2Bzrg; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598"
        }
        res = requests.get(baidu_url,headers=headers)
        html = BeautifulSoup(res.text,'lxml')
        # print(html)
        base = html.find('div',id='content_left')
        arr = base.find_all('div','result c-container')

        next = base.find('a',class_='n')
        print(next)

        for item in arr:
            abstract = item.find('div',class_='c-abstract').text.strip()
            title = item.find('h3',class_='t').text.strip()
            if title:
                data = {}
                url = item.find('a')['href'].strip()
                if "新宝" in title or '东菱' in title:
                    flag = self.emotionAnalysis(title)
                    if flag:
                        data['title'] = title
                        data['url'] = url
                        data['abstract'] = abstract
                        # print(title)
                        # print(url)
                        # print(abstract)
                        result.append(data)
                        return result
                if "新宝" in abstract or '东菱' in abstract:
                    flag = self.emotionAnalysis(abstract)
                    if flag:
                        data['title'] = title
                        data['url'] = url
                        data['abstract'] = abstract
                        # print(title)
                        # print(url)
                        # print(abstract)
                        result.append(data)
                        return result
    def emotionAnalysis(self,contents):
        if not contents:
            contents = ''
        if type(contents)==list:
            for content in contents:
                flag = Emotion().start(content)
        else:
            flag = Emotion().start(contents)
        if flag == 1:
            return True
        else:
            return False
# for i in range(1000,1010):
#     print(i)
#     st = str(i)
#     if i == 0:
#         st = ""
#     ReptleBaiduComment().start("东菱电器质量怎么样",st)


res =  ReptleBaiduComment().start("东菱烤箱怎么样",'30')
print(res)